package com.dacool.http;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dacool.model.CartItem;

public class CalculateFeeRequest extends BaseRequest {

	private ArrayList<CartItem> goodsList;
	private int addressId;
	private int shippingId;

	public CalculateFeeRequest setGoodsList(ArrayList<CartItem> goodsList) {
		this.goodsList = goodsList;
		return this;
	}

	public CalculateFeeRequest setAddressId(int addressId) {
		this.addressId = addressId;
		return this;
	}

	public CalculateFeeRequest setShippingId(int shippingId) {
		this.shippingId = shippingId;
		return this;
	}

	@Override
	protected boolean isGet() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected String url() {
		// TODO Auto-generated method stub
		return "calcShipCost.jsn";
	}

	@Override
	protected void generateParams(HashMap<String, Object> map) {
		JSONArray array = new JSONArray();
		for (CartItem item : goodsList) {
			JSONObject itemObject = new JSONObject();
			/**
			 * &shipping=快递&shippingId=1&shipCost=10&markText=9点到10点
			 */
			try {
				itemObject.put("goodsId", item.getProduct().getId());
				itemObject.put("nums", item.getAmount());

				array.put(itemObject);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		map.put("goodsList", array.toString());
		map.put("addressId", addressId);
		map.put("shippingId", shippingId);
	}

}
